/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.bsc.logs.util;

import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.joran.JoranConfigurator;
import ch.qos.logback.core.util.StatusPrinter;
import java.io.File;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author penn
 */
public class LoadLogback {

    private static final Logger logger = LoggerFactory.getLogger(LoadLogback.class);
    public LoadLogback(){}

    public LoadLogback(String paths) {
        try {
            
            LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
            loggerContext.reset();
            JoranConfigurator joranConfigurator = new JoranConfigurator();
            joranConfigurator.setContext(loggerContext);
            joranConfigurator.doConfigure(LoadLogback.class.getResource(paths));
            //print logger context status
            StatusPrinter.printInCaseOfErrorsOrWarnings(loggerContext);
            logger.debug("loaded slf4j configure file from {}", paths);

        } catch (Exception e) {
            logger.error("con't loading logback configure file from " + paths, e);
        }
    }
    
    public static void main(String[] args){
        
        String paths = "/com/bsc/logs/logback/logback.xml";
//        new LoadLogback(paths);
        try {
            System.out.println("load1111: " + paths);
            LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
            System.out.println("2 "+logger);
            loggerContext.reset();
            JoranConfigurator joranConfigurator = new JoranConfigurator();
            System.out.println("3 "+joranConfigurator);
            joranConfigurator.setContext(loggerContext);
            System.out.println("4 "+joranConfigurator.getContext().getName());
            System.out.println(LoadLogback.class.getResource(paths));
            joranConfigurator.doConfigure(LoadLogback.class.getResource(paths));
            StatusPrinter.printInCaseOfErrorsOrWarnings(loggerContext);
            System.out.println("1 "+logger);
            logger.info("loaded slf4j configure file from {}", paths);
            logger.debug("loaded slf4j configure file from {}", paths);

        } catch (Exception e) {
            logger.error("con't loading logback configure file from " + paths, e);
        }
        
    }
}
